#!/bin/bash
set -e

cat << EOF > ingress-nginx-config.yaml
apiVersion: apps.sealos.io/v1beta1
kind: Config
metadata:
  creationTimestamp: null
  name: ingress-nginx-config
spec:
  data: |
    controller:
      hostNetwork: true
      kind: DaemonSet
      service:
        type: NodePort
  match: dockerproxy.com/labring/ingress-nginx:v1.8.0
  path: charts/ingress-nginx/values.yaml
  strategy: merge
EOF

sealos run dockerproxy.com/labring/kubernetes-reflector:v7.0.151 \
    dockerproxy.com/labring/ingress-nginx:v1.8.0 \
    dockerproxy.com/labring/zot:v1.4.3 \
    dockerproxy.com/labring/kubeblocks:v0.6.0 \
    --env policy=anonymousPolicy \
    --config-file ingress-nginx-config.yaml

echo "patch ingress-nginx-controller tolerations to allow run on master node, if you don't want to run on master node, please ignore this step"
kubectl -n ingress-nginx patch ds ingress-nginx-controller -p '{"spec":{"template":{"spec":{"tolerations":[{"key":"node-role.kubernetes.io/control-plane","operator":"Exists","effect":"NoSchedule"}]}}}}'

echo "waitting for kubeblocks crd created, this may take a while"
while ! kubectl get clusterdefinitions.apps.kubeblocks.io redis >/dev/null 2>&1; do
  sleep 5
done

echo "start patch redis clusterdefinition"
kubectl patch clusterdefinitions.apps.kubeblocks.io redis --type='json' -p '[{"op": "add", "path": "/spec/componentDefs/0/podSpec/containers/1/resources/limits", "value": {"cpu":"100m", "memory":"100Mi"}}]'
echo "patch redis success"

echo "wait for all pod to be ready then install Sealos"
kubectl get po -A